<?php
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if($op == "display"){
   $list=pdo_fetchall('select * from '.tablename('activity_article').' where uniacid=:uniacid and openid=:openid order by id desc ',array(':uniacid'=>$_W['uniacid'],':openid'=>$openid));
   foreach($list as $k=>$v){
    $list[$k]['article_img']="http://wx.lanxuevip.com/attachment/".$v['article_img'];
    $list[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
   }
   include $this->template('article/list');
}
if($op == "list_agree"){
   $list=pdo_fetchall('select * from '.tablename('activity_article').' where uniacid=:uniacid  and status=1 order by visittime desc ',array(':uniacid'=>$_W['uniacid']));
   foreach($list as $k=>$v){
    $authorinfo=pdo_fetch('select * from '.tablename('ewei_shop_member')." where uniacid=:uniacid and openid=:openid ",array(':uniacid'=>$_W['uniacid'],':openid'=>$v['openid']));
    $list[$k]['article_img']="http://wx.lanxuevip.com/attachment/".$v['article_img'];
    $list[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
    $list[$k]['nickname']=$authorinfo['nickname'];
   }
   include $this->template('article/list_agree');
}
if($op == "editor"){
        //编辑文章
        $id=$_GPC['id'];
        if(!empty($id)){
        $info=pdo_fetch('select * from '.tablename('activity_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$id));
            if(!empty($info)){
            $info['image']="http://wx.lanxuevip.com/attachment/".$info['article_img'];
            $info['article_content']=htmlspecialchars_decode($info['article_content']);
            }
        }
        if($_GPC['data']){
            $data['article_img']=$_GPC['data']['article_img'];
            $data['article_title']=$_GPC['data']['article_title'];
            $data['article_content']=$_GPC['data']['article_content'];
            $data['uniacid']=$_W['uniacid'];
            $data['openid']=$openid;
            $data['createtime']=time();
            if(!empty($_GPC['data']['article_id'])){
                //更新文章
                $article_info=pdo_fetch('select * from '.tablename('activity_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$_GPC['data']['article_id']));
                //判断是否存在这篇文章和是否审核
                if(!empty($article_info) && $article_info['status']==0){
                pdo_update('activity_article',$data,array('id'=>$_GPC['data']['article_id'],'uniacid'=>$_W['uniacid']));
                show_json(1,array('article_id'=>$_GPC['data']['article_id']));
                }else{
                show_json(0);  
                }
            }else{
                pdo_insert('activity_article', $data);
                $article_id = pdo_insertid();
                if($article_id){
                show_json(1,array('article_id'=>$article_id));
                }else{
                show_json(0);  
                }
            }
        }
  include $this->template('article/editor');  
}
if($op == "detail"){
  $id=$_GPC['id'];
  if(empty($id)){
     exit; 
  }
  $info=pdo_fetch('select * from '.tablename('activity_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$id));
  if(empty($info)){
  echo "该文章已删除";exit; 
  }
  $authorinfo=pdo_fetch('select * from '.tablename('ewei_shop_member')." where uniacid=:uniacid and openid=:openid ",array(':uniacid'=>$_W['uniacid'],':openid'=>$info['openid']));
  $info['article_content']=htmlspecialchars_decode($info['article_content']);
  $info['createtime']=date('Y-m-d H-i-s',$info['createtime']);
  $info['author']=$authorinfo['nickname'];
  $info['author_id']=$authorinfo['id'];
  $info['image']="http://wx.lanxuevip.com/attachment/".$info['article_img'];
  include $this->template('article/detail'); 
}
if($op == "del"){
  $id=$_GPC['id'];
  if(empty($id)){
     exit; 
  }
  $info=pdo_fetch('select * from '.tablename('activity_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$id));
  if(!empty($info)){
  pdo_delete("activity_article", array('uniacid'=>$_W['uniacid'],'id'=>$id));
  }
  $redirect=$this->createMobileUrl('article/list',array('op'=>'editor'));
  header('location: '.$redirect);
}